package com.example.hotelback.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelback.entity.Params;
import com.example.hotelback.entity.User;
import com.example.hotelback.exception.CustomException;
import com.example.hotelback.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;


@Service
public class UserService {

    @Autowired // 按类型注入
    private UserMapper userMapper;

    public User findByCond(String name, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("password", password);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null) {
            throw new CustomException("用户名或密码错误");
        }
        return user;
    }

    public User findById(int id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return userMapper.selectOne(queryWrapper);
    }

    public User register(User user) {
        String tempName = user.getName();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", tempName);
        if(userMapper.selectOne(queryWrapper) != null) {
            throw new CustomException("该用户名已被占用，请重新输入");
        } else {
            User registerUser = new User(user);
            registerUser.setIsRoot(0);
            registerUser.setIsDelete(0);
            int row = userMapper.insert(registerUser);
            if(row < 0) {
                throw new CustomException("注册失败，请重新注册");
            }
            return registerUser;
        }
    }

    public IPage<User> findAllUsers(Params params) {
        Page<User> page = new Page<>(params.getCurrent(), params.getSize());
        IPage<User> iPage = userMapper.selectPage(page, null);
        return iPage;
    }

    public void update(int id, User newUser) {
        User user = userMapper.selectById(id);
        Field[] fields = User.class.getDeclaredFields();
        boolean hasUpdate = false;
        for (Field field : fields) {
            field.setAccessible(true); // 暴力反射，忽略权限修饰符
            Object o1 = null; // 新
            Object o2 = null; // 原
            try {
                o1 = field.get(newUser);
                o2 = field.get(user);
            } catch (IllegalAccessException e) {
                throw new CustomException(e.getMessage());
            }
            try {
                if (o1 != null && !o1.equals(o2)) {
                    field.set(user, o1);
                    hasUpdate = true;
                }
            } catch (IllegalAccessException e) {
                throw new CustomException(e.getMessage());
            }
        }
        // 如果有字段更新
        if(hasUpdate) {
            userMapper.updateById(user);
        }
    }
}
